﻿using System;
using System.Collections.Generic;
using System.Linq;
using Affinity.Core.Exceptions;

namespace Affinity.Core.Validation
{
    public static class Assert
    {
        public static void ArgumentNotEmpty<T>(IEnumerable<T> value, string valueName, string customMessage = null)
        {
            if (value == null || !value.Any()) ThrowHelper.Throw(new ArgumentNullException(valueName, customMessage ?? (valueName + " may not be empty.")), 1);
        }

        public static void ArgumentNotNull<T>(T value, string valueName, string customMessage = null)
            where T : class
        {
            if (value == null) ThrowHelper.Throw(new ArgumentNullException(valueName, customMessage ?? (valueName + " may not be null.")), 1);
        }

        public static void ArgumentNotNull<T>(T? value, string valueName, string customMessage = null)
            where T : struct
        {
            if (value == null) ThrowHelper.Throw(new ArgumentNullException(valueName, customMessage ?? (valueName + " may not be null.")), 1);
        }

        public static void IsFalse(bool value, string failureMessage)
        {
            if (value) ThrowHelper.Throw(new Exception(failureMessage), 1);
        }

        public static void IsTrue(bool value, string failureMessage)
        {
            if (!value) ThrowHelper.Throw(new Exception(failureMessage), 1);
        }

        public static void IsType<T>(object value, string valueName, bool allowNull = false, string customMessage = null)
        {
            if ((!allowNull && value == null) || !(value is T)) ThrowHelper.Throw(new Exception(customMessage ?? string.Format("{0} must be of type {1}.", valueName, typeof(T))), 1);
        }

        public static void NotEmpty<T>(IEnumerable<T> value, string valueName, string customMessage = null)
        {
            if (value == null || !value.Any()) ThrowHelper.Throw(new Exception(customMessage ?? (valueName + " may not be empty.")), 1);
        }

        public static void NotEmpty(Guid value, string valueName, string customMessage = null)
        {
            if (value == Guid.Empty) ThrowHelper.Throw(new Exception(customMessage ?? (valueName + " may not be empty.")), 1);
        }

        public static void NotNull<T>(T value, string valueName, string customMessage = null)
            where T : class
        {
            if (value == null) ThrowHelper.Throw(new Exception(customMessage ?? (valueName + " may not be null.")), 1);
        }

        public static void NotNull<T>(T? value, string valueName, string customMessage = null)
            where T : struct
        {
            if (value == null) ThrowHelper.Throw(new Exception(customMessage ?? (valueName + " may not be null.")), 1);
        }
    }
}
